package com.example.vv.fragmentWork.mvp;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.vv.fragmentWork.base.BaseFragment;
import com.example.vv.fragmentWork.base.BaseView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentActivity;
import butterknife.ButterKnife;

public abstract class MvpFragment<V extends BaseView,P extends MvpPresenter> extends BaseFragment {
    protected  P presenter;
    public FragmentActivity mActivity;
    private boolean isInitView = false;
    private boolean isVisible = false;

    public abstract int initLayout();
    public abstract P initPresenter();
    public abstract void initView();
    public abstract void lazyLoad();
    public abstract void initData();
    public abstract void doBusiness();
    public abstract View titleBar();

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(initLayout(),container,false);
        mActivity = getActivity();
        presenter = initPresenter();
        presenter.attach((V)this);
        ButterKnife.bind(this,view);
        initData();
        initView();
        doBusiness();
        isInitView = true;
        isCanLoadData();
        return view;
    }
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        //isVisibleToUser这个boolean值表示:该Fragment的UI 用户是否可见，获取该标志记录下来
        if(isVisibleToUser){
            isVisible = true;
            isCanLoadData();
        }else{
            isVisible = false;
        }
    }

    private void isCanLoadData(){
        //所以条件是view初始化完成并且对用户可见
        if(isInitView && isVisible ){
            lazyLoad();
            //防止重复加载数据
            isInitView = false;
            isVisible = false;
        }
    }
}
